<script setup lang="ts">
import { getHomeGoodsGuessLikeAPI } from '@/services/home'
import type { PageParams } from '@/types/global'
import type { GuessItem } from '@/types/home'
import { onMounted, ref } from 'vue'

// 分页参数
const pageParams: Required<PageParams> = {
  page: 1,
  pageSize: 10,
}

// 猜你喜欢的列表
const guessList = ref<GuessItem[]>([])
// 模拟数据
/*const guessList = ref<GuessItem[]>([
    {
    id: '1',
    desc: '超薄手表男士休闲简约夜光石英防水直径40毫米',
    discount: 0.8,
    name: '德国THORE男表 超薄手表男士休闲简约夜光石英防水直径40毫米',
    orderNum: 990,
    picture: 'https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/uploads/goods_preview_1.jpg',
    price: 899
    },
    {
      id: '2',
      desc: '超薄手表男士休闲简约夜光石英防水直径40毫米',
      discount: 0.8,
      name: '德国THORE男表 超薄手表男士休闲简约夜光石英防水直径40毫米',
      orderNum: 990,
      picture: 'https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/uploads/goods_preview_2.jpg',
      price: 899
    },
    {
    id: '3',
    desc: '超薄手表男士休闲简约夜光石英防水直径40毫米',
    discount: 0.8,
    name: '德国THORE男表 超薄手表男士休闲简约夜光石英防水直径40毫米',
    orderNum: 990,
    picture: 'https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/uploads/goods_preview_1.jpg',
    price: 899
    },
    {
      id: '4',
      desc: '超薄手表男士休闲简约夜光石英防水直径40毫米',
      discount: 0.8,
      name: '德国THORE男表 超薄手表男士休闲简约夜光石英防水直径40毫米',
      orderNum: 990,
      picture: 'https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/uploads/goods_preview_2.jpg',
      price: 899
    },
])*/
// 已结束标记
const finish = ref(false)
// 获取猜你喜欢数据
const getHomeGoodsGuessLikeData = async () => {
  // 退出分页判断
  if (finish.value === true) {
    return uni.showToast({ icon: 'none', title: '没有更多数据~' })
  }
  try{
  const res = await getHomeGoodsGuessLikeAPI(pageParams)
  // 数组追加
  guessList.value.push(...res.result.items)
  // 分页条件
  if (pageParams.page < res.result.pages) {
    // 页码累加
    pageParams.page++
    // console.log('page',pageParams.page,res.result.pages,pageParams.page < res.result.pages)
  } else {
    finish.value = true
  }
  }
  catch(error){
  console.log('error',error)
  finish.value = true
  }
}
// 猜你喜欢
import type { XtxGuessInstance } from '@/types/components'
// 获取猜你喜欢组件实例
const guessRef = ref<XtxGuessInstance>()

// 重置数据
const resetData = () => {
  pageParams.page = 1
  guessList.value = []
  finish.value = false
}
// 组件挂载完毕
onMounted(() => {
  getHomeGoodsGuessLikeData()
})
// 暴露方法
defineExpose({
  resetData,
  getMore: getHomeGoodsGuessLikeData,
})
</script>

<template>
  <!-- 猜你喜欢 -->
  <view class="caption">
    <text class="text">猜你喜欢</text>
  </view>
  <view class="guess">
    <navigator
      class="guess-item"
      v-for="item in guessList"
      :key="item.id"
      :url="`/pages/goods/goods?id=${item.id}`"
    >
      <image class="image" mode="aspectFill" :src="item.picture"></image>
      <view class="name"> {{ item.name }} </view>
      <view class="price">
        <text class="small">¥</text>
        <text>{{ item.price }}</text>
      </view>
    </navigator>
  </view>
  <view class="loading-text">
    {{ finish ? '没有更多数据~' : '正在加载...' }}
  </view>
</template>

<style lang="scss">
@import './styles/XtxGuess.scss';
</style>